package com.liming.auth.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liming.auth.mapper.RolePermissionMapper;
import com.liming.auth.service.RolePermissionService;
import com.liming.common.pojo.entity.authinfo.RolePermission;
import com.liming.common.util.mybatisplus.MPUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liming
 * @date 2025/1/20
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission>
        implements RolePermissionService, MPUtil<RolePermission> {
    @Override
    public List<Long> listByRoleId(Long roleId) {
        return lambdaQuery().eq(RolePermission::getRoleId, roleId).list().stream().map(RolePermission::getPermissionId).toList();
    }

    @Override
    public List<Long> userPermissionRoleIds(Long permissionId) {
        return lambdaQuery().eq(RolePermission::getPermissionId, permissionId).list().stream().map(RolePermission::getRoleId).toList();
    }

    @Override
    public Boolean removeByRoleId(Long id) {
        List<RolePermission> list = lambdaQuery().eq(RolePermission::getRoleId, id).list();
        if (CollUtil.isEmpty(list)) {
            return true;
        }
        return lambdaUpdate().eq(RolePermission::getRoleId, id).remove();
    }

    @Override
    public Boolean removeByPermissionId(Long id) {
        List<RolePermission> list = lambdaQuery().eq(RolePermission::getPermissionId, id).list();
        if (CollUtil.isEmpty(list)) {
            return true;
        }
        return lambdaUpdate().eq(RolePermission::getPermissionId, id).remove();
    }

    @Override
    public List<Long> permissionIdsByRoleIds(List<Long> roleIdList) {
        return lambdaQuery().in(RolePermission::getRoleId, roleIdList).list()
                .stream()
                .map(RolePermission::getPermissionId)
                .toList();
    }
}
